<script lang="ts" setup name="KnowledgeList">
import type { KnowledgeType, KnowledgeList, KnowledgeParams } from '@/types/consult'
import KnowledgeItem from './knowledge-item.vue'
import { getKnowledgeList } from '@/api/consult'
import { ref } from 'vue'
const { type } = defineProps<{
  type: KnowledgeType
}>()
const list = ref<KnowledgeList>([])
const loading = ref(false)
const finished = ref(false)
// 第一次，或是上拉加载的时候，都会执行该方法
const params: KnowledgeParams = {
  type,
  current: 1,
  pageSize: 10
}
const onLoad = async () => {
  const res = await getKnowledgeList(params)
  // 累加
  list.value.push(...res.data.rows)
  // 判断是否还有更多数据
  if (list.value.length < res.data.total) {
    // 还有更多数据
    params.current++
  } else {
    finished.value = true
  }

  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      @load="onLoad"
      :finished="finished"
      finished-text="没有更多了"
    >
      <KnowledgeItem v-for="item in list" :key="item.id" :item="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
